﻿using AutoMapper;
using B.Z.BasicData.Domain.Entity.Excused;
using B.Z.BasicData.ErrorCode;
using B.Z.BasicData.Infrastructure;
using MediatR;

namespace B.Z.BasicData.API.Write.Application.ExcusedManage
{
    public class ExcuseUpdateOrRemoveCommandHandler : IRequestHandler<ExcuseUpdateOrRemoveCommand, APIResult<int>>
    {
        private readonly IBaseRepository<T_Excused> excuseRepository;
        private readonly IMapper mapper;

        public ExcuseUpdateOrRemoveCommandHandler(IBaseRepository<T_Excused> excuseRepository, IMapper mapper)
        {
            this.excuseRepository = excuseRepository;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(ExcuseUpdateOrRemoveCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code = APIEnum.ok;
            var model = mapper.Map<T_Excused>(request);
            model.UpdateTime = DateTime.Now;
            if (request.IsUpdateOrDel == false)
            {
                result.Message = "修改成功";
                await excuseRepository.Update(model);
                return await Task.FromResult(result);
            }
            else 
            {
                result.Message = "删除成功";
                model.IsDelete = true;
                await excuseRepository.Update(model);
                return await Task.FromResult(result);
            }
        }
    }
}
